package com.raidpoints.client;

import java.util.HashMap;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;


public class AuthBox extends DialogBox {
	TextBox textBox;

	public AuthBox(final RaidPoints rp) {
		setHTML("Login:");
		final Button btnLogin = new Button("Login");
	
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("240px", "140px");
		
		textBox = new TextBox();
		layoutPanel.add(textBox);
		layoutPanel.setWidgetLeftWidth(textBox, 74.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(textBox, 9.0, Unit.PX, 25.0, Unit.PX);
		
		final PasswordTextBox passwordTextBox = new PasswordTextBox();
		passwordTextBox.addKeyPressHandler(new KeyPressHandler(){
			public void onKeyPress(KeyPressEvent event) {
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
					btnLogin.click();
				}
			}
			
		});
		layoutPanel.add(passwordTextBox);
		layoutPanel.setWidgetLeftWidth(passwordTextBox, 74.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(passwordTextBox, 40.0, Unit.PX, 25.0, Unit.PX);
		
		Label lblUser = new Label("User:"); 
		layoutPanel.add(lblUser);
		layoutPanel.setWidgetLeftWidth(lblUser, 12.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblUser, 16.0, Unit.PX, 18.0, Unit.PX);
		
		Label lblPassword = new Label("Password:");
		layoutPanel.add(lblPassword);
		layoutPanel.setWidgetLeftWidth(lblPassword, 12.0, Unit.PX, 70.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblPassword, 47.0, Unit.PX, 18.0, Unit.PX);
		
		
		btnLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				rp.jli = true;
				rp.rpsvc.AuthUser(textBox.getText(), passwordTextBox.getText(), rp.authCallback);
				hide();
			}
		});
		layoutPanel.add(btnLogin);
		layoutPanel.setWidgetLeftWidth(btnLogin, 22.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnLogin, 96.0, Unit.PX, 30.0, Unit.PX);
		
		Button btnCancel = new Button("Cancel");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		layoutPanel.add(btnCancel);
		layoutPanel.setWidgetLeftWidth(btnCancel, 133.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnCancel, 96.0, Unit.PX, 30.0, Unit.PX);
		textBox.setFocus(true);
		
		Label createLabel = new Label("Click to create new account");
		createLabel.setStyleName("loginlabel");
		layoutPanel.add(createLabel);
		layoutPanel.setWidgetLeftWidth(createLabel, 34.0, Unit.PX, 180.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(createLabel, 71.0, Unit.PX, 16.0, Unit.PX);
		
		createLabel.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				NewAccountBox nab = new NewAccountBox(rp);
				hide(); nab.center(); nab.show();
			}
		});
	}
}
